var Protomean_Explorer = Class.create();

Protomean_Explorer.prototype = {

	initialize : function() {
	
		this._nl = "\r\n";
		
	}
,
	initIframe : function(iframe) {
		// avoid multiinitialization (IE Bug)


		this._iframe = iframe;
		this._doc = iframe.contentWindow.document;

		this._doc.title = this._index;


		//init html value
		this._doc.body.innerHTML = this._el.value || this._opts.html;

		this.skin();


		if (this._initialized)
			return;
		this._initialized = true;




		//init designMode
		this.enableDesignMode();

		//pre-bind functions

		//bind external events
		this.bindEvents();
		
		var saveCaret = this.saveCaret.bindAsEventListener(this);

		Event.observe(this._doc, "beforedeactivate", saveCaret);
		Event.observe(this._doc, "click", saveCaret);

		//bind editor keydown events
		Event.observe(this._doc, "keydown", this.keydown.bindAsEventListener(this));

		//bind editor keyup events
		Event.observe(this._doc, "keyup", this.keyup.bindAsEventListener(this));

		//bind editor keyup events
		Event.observe(this._doc, "keypress", this.keypress.bindAsEventListener(this));

		//bind editor focus events (used to reset designmode - Gecko bug)
		Event.observe(this._doc, "focus", this.focus.bindAsEventListener(this));

		Event.observe(this._doc, "click", this.focus.bindAsEventListener(this));
		Event.observe(this._doc, "keyup", this.focus.bindAsEventListener(this));

    //post-init functions

    //add event listeners to doc elements, e.g. images
    // this.listen();
	}

,

	enableDesignMode : function() {
		// if (this._doc.designMode == "off") {
			this._doc.designMode = "on";
			// this._doc.execCommand("styleWithCSS", '', false);
		// }
	}
,
	keydown : function() {
		// alert("tralala");
	}
,
	keyup : function() {
		this.saveCaret();
	}
,
	keypress : function(e) {
		if(e.keyCode == 13 && !e.shiftKey) {
			// RETURN key
			// cleanup <br><br> between paragraphs
			// getElementsBySelector does not work in iframes
			// $(this._doc.body).getElementsBySelector('br').remove();
			// use verbose methods instead
			$A(this._doc.body.childNodes)
				.each(function(el) {
					if (el.tagName && el.tagName.toLowerCase() == 'br')
						el.parentNode.removeChild(el);
				});
		}
	
	  var container = this.selected();
		
		var name = container.tagName.toLowerCase();

    //fix forbidden main containers
    if(
      name == "strong" ||
      name == "b" ||
      name == "em" ||
      name == "i" ||
      name == "sub" ||
      name == "sup" ||
      name == "a"

    ) name = container.parentNode.tagName.toLowerCase();

    if(name == "body") this._exec('FormatBlock', 'P');

		var ed = this;

		setTimeout(function() { ed.handleNodeChange(); }, 0);
		
		// this.handleNodeChange();
    
	
	}
,
	focus: function(e) {
		var ed = this;
		this.enableDesignMode();
		setTimeout(function() { ed.handleNodeChange(); }, 0);
	}
};

Protomean_Explorer.prototype.selected = function() {

    var caretPos = this._iframe.contentWindow.document.caretPos;
		var html = caretPos.htmlText;
		// reduce selection is last character is a space
		// (IE selects the word + the next space when double clicking)
		if (html.lastIndexOf(' ') == html.length - 1) {
			caretPos.moveEnd('character', -1);
		}
    if(caretPos!=null) {
			if(caretPos.parentElement!=undefined)
				return(caretPos.parentElement());
		}
};

Protomean_Explorer.prototype.saveCaret = function() {
    this._doc.caretPos = this._doc.selection.createRange();
};


Protomean_Explorer.prototype.setFocusToNode = function(node) {
    var range = this._doc.selection.createRange();
    range.moveToElementText(node);
    range.collapse(false);
    range.move('character',-1);
    range.select();
    node.focus();
};